package com.imooc.spring.aop.aspect;

import org.aspectj.lang.JoinPoint;

import java.text.SimpleDateFormat;
import java.util.Date;

//切面类
public class MethodAspect{
    //切面方法，用于扩展额外功能
    //JoinPoint：连接点，通过临界点可以获取目标类|方法的信息
    public void printExecutionTime(JoinPoint joinPoint) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String now = sdf.format(new Date());
        //获取连接点的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取连接点的方法名
        String methodName = joinPoint.getSignature().getName();
        System.out.println("---->" + now + ":" + className + "." + methodName);
        Object[] args = joinPoint.getArgs();
        System.out.println("---->参数个数:" + args.length);
        for (int i = 0; i < args.length; i++) {
            System.out.println("---->参数" + i + ":" + args[i]);
        }
    }
    public void doAfterReturning(JoinPoint joinPoint, Object ret) {
        System.out.println("<-------返回后通知：" + ret);
    }
    public void doAfter(JoinPoint joinPoint) {
        System.out.println("<-------触发后置通知");
    }
    public void doAfterThrowing(JoinPoint joinPoint, Throwable ex) {
        System.out.println("<-------触发异常通知：" + ex.getMessage());
    }
}
